﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.Unity;
using System.Data;


    public interface IContainerAccessor
    {
        IUnityContainer Container { get; }
    }

    public class Data
    {
        Database db;
        public Database DB
        {
            get
            {
                return db;
            }
            set
            {
                db = value;
            }
        }
        

        public Data()
        {
            var accessor = HttpContext.Current.ApplicationInstance as IContainerAccessor;
            if (accessor == null)
            {
                return;
            }
            var container = accessor.Container;
            if (container == null)
            {
                throw new InvalidOperationException("No Unity container found");
            }

            lock (container)
            {
                db = container.Resolve<Database>();
            }
        }

        public List<ServiceTypeDTO> GetServiceTypes()
        {

            IRowMapper<ServiceTypeDTO> rowMapper = MapBuilder<ServiceTypeDTO>.MapAllProperties()
                                            .Build();

            List<ServiceTypeDTO> s = new List<ServiceTypeDTO>();
            s = db.ExecuteSprocAccessor<ServiceTypeDTO>("dbo.GetServiceTypes", rowMapper).ToList<ServiceTypeDTO>();
            return s;
        }

        public List<ServiceDTO> GetServices()
        {

            IRowMapper<ServiceDTO> rowMapper = MapBuilder<ServiceDTO>.MapAllProperties()
                                            .Build();

            List<ServiceDTO> s = new List<ServiceDTO>();
            s = db.ExecuteSprocAccessor<ServiceDTO>("dbo.GetServices", rowMapper).ToList<ServiceDTO>();
            return s;
        }

        public List<PaymentTypeDTO> GetPaymentTypes()
        {

            IRowMapper<PaymentTypeDTO> rowMapper = MapBuilder<PaymentTypeDTO>.MapAllProperties()
                                            .Build();

            List<PaymentTypeDTO> p = new List<PaymentTypeDTO>();
            p = db.ExecuteSprocAccessor<PaymentTypeDTO>("dbo.GetPaymentTypes", rowMapper).ToList<PaymentTypeDTO>();
            return p;
        }


        public List<CreditCardTypeDTO> GetCreditCardTypes()
        {

            IRowMapper<CreditCardTypeDTO> rowMapper = MapBuilder<CreditCardTypeDTO>.MapAllProperties()
                                            .Build();

            List<CreditCardTypeDTO> p = new List<CreditCardTypeDTO>();
            p = db.ExecuteSprocAccessor<CreditCardTypeDTO>("dbo.GetCreditCardTypes", rowMapper).ToList<CreditCardTypeDTO>();
            return p;
        }

        public List<PriestDTO> GetPriests()
        {

            IRowMapper<PriestDTO> rowMapper = MapBuilder<PriestDTO>.MapAllProperties()
                                            .Build();

            List<PriestDTO> p = new List<PriestDTO>();
            p = db.ExecuteSprocAccessor<PriestDTO>("dbo.GetPriests", rowMapper).ToList<PriestDTO>();
            return p;
        }



        public int AddServiceRequest(ServiceRequestDTO devoteeServiceDTO)
        {

            int Service_Request_ID = Convert.ToInt32(db.ExecuteScalar("dbo.ServiceRequest_Insert", new object[] { devoteeServiceDTO.Devotee_ID, devoteeServiceDTO.Service_ID, devoteeServiceDTO.Service_Date, devoteeServiceDTO.Service_Fee_Paid, devoteeServiceDTO.Payment_Type_id, devoteeServiceDTO.Check_Number, devoteeServiceDTO.Priest_Id, null, null, devoteeServiceDTO.Service_Request_ID }));

            //int c = db.ExecuteNonQuery("dbo.Devotee_AddEditService", new object[] { devoteeServiceDTO.Devotee_ID, devoteeServiceDTO.Service_ID, devoteeServiceDTO.Service_Date, devoteeServiceDTO.Service_Fee_Paid, devoteeServiceDTO.Payment_Type_id, devoteeServiceDTO.Check_Number, devoteeServiceDTO.Priest_Id, null, null, devoteeServiceDTO.Service_Request_ID, error });

            //DataTable table = db.ExecuteDataSet(cmd).Tables[0];
            //IDataReader reader = table.CreateDataReader();
            //int x = db.GetParameterValue("@po_iOut");
            //return reader;


            return Service_Request_ID;
        }

        public void DeleteServiceRequest(int Service_Request_ID)
        {
            int reqId = Convert.ToInt32(db.ExecuteScalar("dbo.ServiceRequest_Delete", new object[] { Service_Request_ID, null }));
            
        }

        public int AddEditServiceReqestTransaction(int Service_Request_ID, int Service_Req_Tran_ID, string transactionType, paymentResponse paymentResponse)
        {
            int c = Convert.ToInt32(db.ExecuteScalar("dbo.ServiceReqest_Transaction_Insert", new object[] { Service_Req_Tran_ID, Service_Request_ID, paymentResponse.TransactionID, transactionType, paymentResponse.CardNumber, paymentResponse.RequestedAmount, paymentResponse.ApprovedAmount, paymentResponse.AuthCode, paymentResponse.AVSResponse, paymentResponse.CardCodeResponse, Convert.ToInt32(paymentResponse.Approved), paymentResponse.Message, paymentResponse.ResponseCode, null, null }));

            return c;
        }

        public List<ServiceRequestDTO> GetServiceRequests(int serviceRequestId, int devoteeId)
        {

            IRowMapper<ServiceRequestDTO> rowMapper = MapBuilder<ServiceRequestDTO>.MapAllProperties()
                                            .Build();

            List<ServiceRequestDTO> requests = new List<ServiceRequestDTO>();
            requests = db.ExecuteSprocAccessor<ServiceRequestDTO>("dbo.ServiceRequest_Select", rowMapper, new object[] { serviceRequestId, devoteeId }).ToList<ServiceRequestDTO>();

            if (requests.Any())
            {
                string requestIds = "";
                requestIds = string.Join(",", (from p in requests select p.Service_Request_ID).ToArray());

                List<ServiceRequestTransactionDTO> list = GetServiceRequestTransaction(0, requestIds);

                foreach (ServiceRequestDTO req in requests)
                {
                    req.Transactions = list.FindAll(t => t.Service_Request_ID == req.Service_Request_ID);
                }
            }

            return requests;
            //if (p.Any())
            //    return p.FirstOrDefault();
            //else
            //    return new ServiceRequestDTO();
        }



        public List<ServiceRequestTransactionDTO> GetServiceRequestTransaction(int serviceReqTranID, string serviceRequestIDs)
        {

            IRowMapper<ServiceRequestTransactionDTO> rowMapper = MapBuilder<ServiceRequestTransactionDTO>.MapAllProperties()
                                            .Build();

            List<ServiceRequestTransactionDTO> p = new List<ServiceRequestTransactionDTO>();
            p = db.ExecuteSprocAccessor<ServiceRequestTransactionDTO>("dbo.ServiceRequest_Transaction_Select", rowMapper, new object[] { serviceReqTranID, serviceRequestIDs }).ToList<ServiceRequestTransactionDTO>();

            return p;
        }


    }